package com.xingchen.springboot3.controller;

import com.xingchen.springboot3.common.bean.Result;
import com.xingchen.springboot3.pojo.dto.PwdDto;
import com.xingchen.springboot3.pojo.dto.UserDto;
import com.xingchen.springboot3.pojo.vo.UserVo;
import com.xingchen.springboot3.service.UserService;
import com.xingchen.springboot3.utils.ThreadLocalUtil;
import jakarta.validation.Valid;
import lombok.RequiredArgsConstructor;
import org.hibernate.validator.constraints.URL;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

/**
 *
 * @author weijingchen
 * @DateTime 2025-09-23
 */
@RestController
@RequestMapping("/user")
@RequiredArgsConstructor

public class UserController {

    private final UserService userService;

    @PostMapping("/register")
    public Result register(@Validated @RequestBody UserDto dto) {
        return userService.register(dto);
    }

    @PostMapping("/login")
    public Result login(@Validated @RequestBody UserDto dto) {
        return userService.login(dto);
    }

    @GetMapping("/userInfo")
    public Result<UserVo> userInfo() {
        Integer userId = ThreadLocalUtil.getId();
        return Result.success(userService.getById(userId));
    }

    @PutMapping("/update")
    public Result update(@Validated @RequestBody UserDto dto) {
        return userService.update(dto);
    }

    /**
     * 修改头像
     * @param avatar
     * @return
     */
    @PatchMapping("/updateAvatar")
    public Result updateAvatar(@RequestParam @URL String avatar) {
        return userService.updateAvatar(avatar);
    }

    /**
     *  修改密码
     * @param dto 修改密码的参数
     * @return 修改密码结果
     */
    @PostMapping("/updatePWD")
    public Result updatePWD(@Validated @RequestBody PwdDto dto) {
        return userService.updatePWD(dto);
    }
}
